﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using CNCSoft.EWinform;
using CNCSoft.EWinform.ControlUtils;
using CNCSoft.EWinform.ERPApps;
using CNCSoft.EWinform.AGrid;
using CNCSoft.EData;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

using System.Threading;
using System.Globalization;

using Common;

namespace CNCSoft.ErpApps.TMS.Academic
{
    public partial class LyLichHocVien : Form
    {
        public LyLichHocVien()
        {
            // Sets the UI culture to VietNamese (VietNam) / English
            if (GlobalVariable.WSInfo.Language == "Tiếng Việt")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("vi-VN");
            else if (GlobalVariable.WSInfo.Language == "English")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");

            InitializeComponent();
        }

        private void txtEnquiryNumber_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtEnquiryNumber.Text == "") return;
                lblTenHV.Text = SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString,
                                        "SELECT distinct HoTenHV FROM View_Invoice " +
                                        "WHERE EnquiryNumber = '" + txtEnquiryNumber.Text + "'").ToString().ToUpper();
            }
            catch { lblTenHV.Text = ""; }
        }


        private void cmdLookupEnquiry_Click(object sender, EventArgs e)
        {
            FormView flookup = null;
            if (flookup == null)
            {
                string strSELECT = @"select distinct view_invoice.EnquiryNumber, HoTenHV, isnull(Malop,'') as Lop
                                    from view_invoice left join hoctap on view_invoice.enquirynumber = hoctap.enquirynumber ";

                string strWHERE = "";
                if (!chkBaoLuu.Checked )
                    strWHERE = strWHERE + " WHERE view_invoice.enquirynumber not in (select enquirynumber from baoluu join invoice on baoluu.invoicenumber = invoice.invoicenumber) ";
                if (!chkBoHoc.Checked && strWHERE == "")
                    strWHERE = strWHERE + " WHERE view_invoice.enquirynumber not in (select enquirynumber from bohoc join invoice on bohoc.invoicenumber = invoice.invoicenumber) ";
                if (!chkBoHoc.Checked && strWHERE != "")
                    strWHERE = strWHERE + " AND view_invoice.enquirynumber not in (select enquirynumber from bohoc join invoice on bohoc.invoicenumber = invoice.invoicenumber) ";

                flookup = new FormView(strSELECT + strWHERE,
                                    GlobalVariable.WSInfo.ConnectionString,
                                   GlobalVariable.WSInfo.WorkingDate);
                //flookup.DateFilterColumns = new string[,] { { "EnquiryDate", "Enquiry date" } };
                //flookup.CollumnFocusInFilterGrid = 2;
            }
            flookup.ShowDialog(this);
            if (flookup.DialogResult == DialogResult.OK)
            {
                //txtEnquiryNumber.Text = flookup.SelectedIDs.Replace("('", "").Replace("')", "").Trim().Substring(0,10)  ;
                //txtEnquiryNumber.Focus();
                
                LoadDanhSachHocVien("WHERE EnquiryNumber IN " + flookup.SelectedIDs);
                
                cmdXem_Click(sender,e);
            }

        }

        
        ReportDocument rpt = new ReportDocument();

        private void View_Report()
        {
            string strMaskViewSub = ""; string str1 = "0"; string str2 = "0" ; string str3 = "0"; string str4="0";
            if (chkFeeplanSub.Checked) str1 = "1"; if (chkTienMatSub.Checked) str2 = "1"; if (chkQuaTrinhHocTapSub.Checked) str3="1"; if (chkKetQuaHocTapSub.Checked) str4 = "1";
            strMaskViewSub = str1 + str2 + str3 + str4 ;
            //MessageBox.Show(strMaskViewSub);
            try
            {
                //Set parameter
                //PublicFunction.SetParameter(rpt, "@pTuNgay", txtTuNgay.getDateValue());
                PublicFunction.SetParameter(rpt, "@pEnquiryNumber", txtEnquiryNumber.Text);
                PublicFunction.SetParameter(rpt, "pMaskViewSub", strMaskViewSub);
                PublicFunction.SetParameter(rpt, "pMoRongDong", chkMoRongDong.Checked);
                
                //Gan report source
                CRViewer.ReportSource = rpt;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "cmdXem error!");
            }
        }

        private void LyLichHocVien_Load(object sender, EventArgs e)
        {
            lblSoItem.Text = "";

            try
            {
                rpt.Load(Application.StartupPath + "\\Report\\Report_LyLichHocVien.rpt");
                PublicFunction.SetDBLogonForReport(rpt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            LoadDanhSachHocVien("");
        }

        private void cmdXem_Click(object sender, EventArgs e)
        {
            View_Report();
        }

        private void chkMoRongDong_CheckedChanged(object sender, EventArgs e)
        {
            cmdXem_Click(sender, e);
        }

        private void chkFeeplanSub_CheckedChanged(object sender, EventArgs e)
        {
            cmdXem_Click(sender, e);
        }

        private void chkTienMatSub_CheckedChanged(object sender, EventArgs e)
        {
            cmdXem_Click(sender, e);
        }

        private void chkQuaTrinhHocTapSub_CheckedChanged(object sender, EventArgs e)
        {
            cmdXem_Click(sender, e);
        }

        private void chkKetQuaHocTapSub_CheckedChanged(object sender, EventArgs e)
        {
            cmdXem_Click(sender, e);
        }


        private void LoadDanhSachHocVien(string strWHERE)
        {
            lstHocVien.Items.Clear();

            if (strWHERE == "")
            {
                if (!chkBaoLuu.Checked)
                    strWHERE = strWHERE + " WHERE enquirynumber not in (select enquirynumber from baoluu join invoice on baoluu.invoicenumber = invoice.invoicenumber) ";
                if (!chkBoHoc.Checked && strWHERE == "")
                    strWHERE = strWHERE + " WHERE enquirynumber not in (select enquirynumber from bohoc join invoice on bohoc.invoicenumber = invoice.invoicenumber) ";
                if (!chkBoHoc.Checked && strWHERE != "")
                    strWHERE = strWHERE + " AND enquirynumber not in (select enquirynumber from bohoc join invoice on bohoc.invoicenumber = invoice.invoicenumber) ";
            }

            SqlDataReader HV_DataReader = SQLHelper.ExecuteReader(GlobalVariable.WSInfo.ConnectionString,
                                                      "SELECT distinct HoTenHV + space(100) + EnquiryNumber, Ten FROM view_invoice  " + strWHERE + " ORDER BY Ten");
            while (HV_DataReader.Read())
            {
                try
                {
                    lstHocVien.Items.Add(HV_DataReader.GetValue(0).ToString().Trim());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            if (lstHocVien.SelectedIndex != -1)
            {
                lstHocVien.SelectedIndex = 0;
                lblSoItem.Text = lstHocVien.Items.Count.ToString("###0") + " Học viên";
            }
        }

        private void lstHocVien_DoubleClick(object sender, EventArgs e)
        {
            
        }

        private void lstHocVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstHocVien.SelectedIndex == -1) return;
            txtEnquiryNumber.Text = lstHocVien.Text.Substring(100).Trim();
            cmdXem_Click(sender, e);
        }

        private void chkBaoLuu_CheckedChanged(object sender, EventArgs e)
        {
            LoadDanhSachHocVien("");
        }

        private void chkBoHoc_CheckedChanged(object sender, EventArgs e)
        {
            LoadDanhSachHocVien("");
        }


    }
}
